<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/5/26
 * Time: 16:21
 */

namespace app\admin\library;


class DbMap
{
    /**
     * @var string[] 后缀映射表
     */
    protected static $suffixArray = [
        '_time', '_image', '_images', '_file', '_files', '_id', '_ids', '_list', '_data', '_switch'
    ];

    /**
     * 数据表结构转换映射
     * @param $data
     * @return array
     */
    public static function structureSwitch($data)
    {
        $result = [];
        if ($data){
            foreach ($data as $value){
                $item ['name']= $value['name'];  //字段名称
                $item ['comment']= $value['description']; //字段备注
                $item ['type']= $value['input_type'];    //字段类型
                $item ['length']= $value['length'];//字段长度，
                $item ['default']= $value['default'] ? $value['default'] : '';//默认值，默认为空
                $item ['is_null']= $value['is_null'] == 'NO';//是否允许为空，默认为true
                $auto_increment = $value['auto_increment'];
                $item['auto_increment'] = eval("return $auto_increment;");
                $is_primary = $value['is_primary'];
                $item['is_primary'] = eval("return $is_primary;");

                array_push($result, $item);
            }
        }

        return $result;
    }

    /**
     * 关联条件转换
     * @param $relation_list
     * @return array
     */
    public static function joinCondition($relation_list)
    {
        $result = [];
        //获取关联模块表名列表
        foreach ($relation_list as $value){
            $item['name'] = $value['table_name'];
            $relation_condition = json_decode($value['relation_condition'], 320);
            $item['join'] = $relation_condition['join_type'];
            $item['relation_type'] = $relation_condition['relation_type'];
            $item['condition']['main'] = $relation_condition['condition']['main'];
            $item['condition']['relation'] = $relation_condition['condition']['relation'];

            array_push($result, $item);
        }
        return $result;
    }

    public static function parseType($column,$columnType)
    {
        $type = '';
        $columnNameSuffix = self::parseSuffix($column);
        if (in_array($columnNameSuffix, self::$suffixArray)){
            /**
             * 根据后缀转换类型
             */
            switch ($columnNameSuffix) {
                case '_id':
                    if ($columnType == 'int' || $columnType == 'varchar')
                        $type = 'select';
                    break;
                case '_ids':
                    if ($columnType == 'varchar')
                        $type = 'select';
                    break;
                case '_list':
                    if ($columnType == 'enum') {
                        $type = 'select';
                    } else if ($columnType == 'set') {
                        $type = 'select';
                    }
                    break;
                case '_data':
                    if ($columnType == 'enum') {
                        $type = 'radio';
                    } else if ($columnType == 'set') {
                        $type = 'checkbox';
                    }
                    break;
                case '_switch':
                    if ($columnType == 'tinyint')
                        $type = 'switch';
                    break;
                case '_time':
                    if ($columnType == 'int' || $columnType == 'datetime')
                        $type = 'datetime';
                    break;
                case '_date':
                    if ($columnType == 'date')
                        $type = 'date';
                    break;
                case '_image':
                    if ($columnType == 'varchar')
                        $type = 'image';
                    break;
                case '_images':
                    if ($columnType == 'varchar')
                        $type = 'image';
                    break;
                case '_file':
                    if ($columnType == 'int' || $columnType == 'datetime')
                        $type = 'file';
                    break;
                case '_files':
                    if ($columnType == 'date')
                        $type = 'file';
                    break;
            }
        }else{
            /**
             * 根据数据类型转换类型
             */
            switch ($columnType) {
                case 'tinyint':
                case 'int':
                    $type = 'int';
                    break;
                case 'char':
                case 'varchar':
                    $type = 'string';
                    break;
                case 'enum':
                    $type = 'select';
                    break;
                case 'set':
                    $type = 'select';
                    break;
                case 'text':
                    $type =  'textarea';
                    break;
                case 'datetime':
                case 'timestamp':
                    $type = 'datetime';
                    break;
                case 'date':
                    $type = 'date';
                    break;
            }
        }

        return $type;
    }

    /**
     * 解析字段名后缀
     * @param string $columnName
     * @return mixed|string
     */
    private static function parseSuffix(string $columnName)
    {
        if (preg_match("/_[a-zA-Z]+$/", $columnName, $match)) {
            return $match[0];
        }
        return '';
    }
}